﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class AdminLog
    {
        public int Add(KeRui.Model.AdminLog model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Flag", SqlDbType.VarChar, 50), new SqlParameter("@Log", SqlDbType.NVarChar, 450), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.UserID;
            parameters[2].Value = model.Flag;
            parameters[3].Value = model.Log;
            parameters[4].Value = model.CreateIP;
            DbHelperSQL.RunProcedure("AdminLog_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public KeRui.Model.AdminLog GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.AdminLog log = new KeRui.Model.AdminLog();
            DataSet set = DbHelperSQL.RunProcedure("AdminLog_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    log.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    log.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                log.Username = set.Tables[0].Rows[0]["Username"].ToString();
                log.Flag = set.Tables[0].Rows[0]["Flag"].ToString();
                log.Log = set.Tables[0].Rows[0]["Log"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    log.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                log.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                return log;
            }
            return null;
        }
    }
}

